package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/4/6 16:28
* @Version : 1.0
* @Description : 会员业务接口实现类
*/
@Service
@Transactional
@Slf4j
public class MemberServiceImpl implements MemberService {
    //依赖注入
    @Autowired
    private MemberDao memberDao;

    /**
     * 会员登录
     * @param telephone
     */
    @Override
    public void smsLogin(String telephone) {
        //调用dao 方法实现登录
        Member member= memberDao.findByTelephone(telephone);
        //判断会员是否存在
        if (member==null){
            //创建一个新会员(将当前时间和手机号)保存到数据库
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            //调用dao 方法 保存数据
            memberDao.add(member);
        }
    }

    @Override
    public Integer findMemberByDate(String beginQueryDate, String endQueryDate) {
       try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = simpleDateFormat.parse(beginQueryDate);
            System.out.println("beginDate = " + beginDate);
            Date endDate = simpleDateFormat.parse(endQueryDate);
            System.out.println("endDate = " + endDate);
            Integer i  = memberDao.findMemberByDate(beginDate, endDate);
            return i;
        } catch (ParseException px) {
            px.printStackTrace();
            return null;
        }

//        Integer i = memberDao.findMemberByTime(beginQueryDate,endQueryDate);
//        System.out.println("members = " + i);
//        return i;
    }

    @Override
    public Map getExcel(String beginQueryDate, String endQueryDate) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = simpleDateFormat.parse(beginQueryDate);
            Date endDate = simpleDateFormat.parse(endQueryDate);
            //获得今天日期
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周第一天日期
            String thisWeek = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获得本月第一天日期
            String thisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //获取与会员有关数据
            Integer Date2DateMember =memberDao.findMemberByDate(beginDate,endDate);
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeek);
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonth);
            Integer totalMember = memberDao.findMemberTotalCount();
            //封装数据到map集合
            Map<String, Object> map = new HashMap<>();
            String time = beginQueryDate+"至"+endQueryDate;
            //会员有关数据
            map.put("Date2DateMember",Date2DateMember);
            map.put("date2date",time);
            map.put("reportDate", today);
            map.put("todayNewMember", todayNewMember);
            map.put("thisWeekNewMember", thisWeekNewMember);
            map.put("thisMonthNewMember", thisMonthNewMember);
            map.put("totalMember", totalMember);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

